package cn.dglydrpy.eshop.website;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;

/**
 * 实现ApplicationListener接口,用于观察SpringBoot的生命周期
 */
@Slf4j
public class MySpringBootInitEventListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            log.info("----------初始化环境变量----------");
        } else if (event instanceof ApplicationPreparedEvent) {
            log.info("----------初始化环境变量完成----------");
        } else if (event instanceof ContextRefreshedEvent) {
            log.info("----------当ApplicationContext初始化或者刷新时触发应用刷新----------");
        } else if (event instanceof ApplicationReadyEvent) {
            log.info("----------应用已启动完成----------");
        } else if (event instanceof ContextStartedEvent) {
            log.info("----------容器调用ConfigurableApplicationContext的Start()方法开始/重新开始容器时触发应用启动----------");
        } else if (event instanceof ContextStoppedEvent) {
            log.info("----------容器调用ConfigurableApplicationContext的Stop()方法停止容器时触发应用停止----------");
        } else if (event instanceof ContextClosedEvent) {
            log.info("----------ApplicationContext被关闭时触发应用关闭,其管理的所有单例Bean都被销毁----------");
        } else if (event instanceof MyEvent){
            log.info("----------MyEvent自定义事件触发,监听到发送给MyEvent的信息:msg="+((MyEvent) event).getMsg()+"----------");
        }
    }

}
